<template>
  <section class="carActivate">
    <el-dialog
      v-dialogDrag
      :visible.sync="curPopupsVisible"
      :title="textMap[popupsState]"
      width="70%"
      :append-to-body="true"
      :close-on-click-modal="false"
      @close="closeEdit"
    >
      <dialogHeader
        slot="title"
        :title="textMap[popupsState]"
        :style-type="$store.state.app.dialogType"
      />

      <div class="filter-container filter-title">车辆信息</div>
      <div class="filter-container filter-params">
        <el-row ref="vailComponentList1" :gutter="26">
          <component
            :is="comp.component"
            v-for="comp in tableComponents1.filter((o) => o.isMust === true)"
            :ref="comp.isRequire ? comp.isRequire + comp.compKey : comp.compKey"
            :key="comp.compKey"
            :validrule="comp.validrule"
            :code-field="comp.codeField"
            :text-field="comp.textField"
            :popups-key="comp.compKey"
            :parent-fileds="comp.parentFileds"
            :code="formField[comp.codeField]"
            :disabled="comp.disabled"
            :o-fields="comp.oFields"
            :options="comp.options"
            :custom-obj="comp.customObj"
            :is-require="comp.isRequire"
            :is-mul="comp.isMul"
            :span="comp.span || 8"
            :label-name="comp.labelName"
            :lookuptype="comp.lookuptype"
            :date-options-type="comp.dateOptionsType"
            @changeCode="getComponentCode"
            @focusCode="getFocusCode"
          />
        </el-row>
      </div>

      <div class="filter-container filter-title">客户信息</div>
      <div class="filter-container filter-params">
        <el-row ref="vailComponentList2" :gutter="26">
          <component
            :is="comp.component"
            v-for="comp in tableComponents2.filter((o) => o.isMust === true)"
            :ref="comp.isRequire ? comp.isRequire + comp.compKey : comp.compKey"
            :key="comp.compKey"
            :validrule="comp.validrule"
            :code-field="comp.codeField"
            :text-field="comp.textField"
            :popups-key="comp.compKey"
            :parent-fileds="comp.parentFileds"
            :code="formField[comp.codeField]"
            :disabled="comp.disabled"
            :o-fields="comp.oFields"
            :options="comp.options"
            :custom-obj="comp.customObj"
            :is-require="comp.isRequire"
            :is-mul="comp.isMul"
            :span="comp.span || 8"
            :label-name="comp.labelName"
            :lookuptype="comp.lookuptype"
            :date-options-type="comp.dateOptionsType"
            @changeCode="getComponentCode"
            @focusCode="getFocusCode"
          />
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons"
          :key="comp.compKey"
          :type="comp.type"
          :icon="comp.icon"
          @click="comp.clickEvent"
        >{{ comp.text }}</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { formMixins } from '@/components/mixins/formMixins'
import { managements } from '@/api/se/bookingManagement'

export default {
  components: {
    dialogHeader: () => import('@/components/dialogHeader')
  },
  // 组件混入对象
  mixins: [formMixins],
  props: {
    popupsVisible: { type: Boolean, default: false },
    // 从网格传入的编辑行数据(json)
    editRowData: { type: Object, default: function() { return {} } },

    customObj: { type: Object, default: () => { return {} } }
  },
  data() {
    const viewPageCode = 'car-Edit'

    return {
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          clickEvent: () => this.save(),
          text: '保存'
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          clickEvent: () => this.closeEdit(1),
          text: '取消'
        }
      ],
      tableComponents: [],
      tableComponents1:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].formComponents
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].formComponents
          : [
            {
              compKey: 'compKey1',
              labelName: 'VIN',
              codeField: 'vin',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              isRequire: true,
              span: 8
            },

            {
              compKey: 'compKey2',
              labelName: '车牌号',
              codeField: 'carLicense',
              textField: 'deliverycityText',
              oFields: 'cityCode,cityName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              isRequire: true,
              span: 8
            },
            {
              compKey: 'compKey3',
              labelName: '车辆用途',
              codeField: 'buyCaruseCode',
              textField: 'buyCaruseText',
              customObj: {
                api: managements.queryYT,
                selectType: '1'
              },
              component: () => import('@/components/org/commonSelect'),
              isMust: true,
              isRequire: true,
              span: 8
            },
            {
              compKey: 'compKey4',
              labelName: '品牌',
              codeField: 'carBrandCode',
              textField: 'carBrandCn',
              customObj: {
                api: managements.queryPP,
                selectType: '1'
              },
              oFields: 'carBrandCode,carBrandCn',
              component: () => import('@/components/org/commonSelect'),
              isMust: true,
              isRequire: true,
              span: 8
            },
            // {
            //   compKey: 'compKey4',
            //   labelName: '品牌',
            //   codeField: 'carBrandCode',
            //   textField: 'carBrandCn',
            //   customObj: {
            //     api: managements.queryPP,
            //     selectType:'1'
            //   },
            //   oFields: 'carBrandCode,carBrandCn',
            //   type: 'dropdownList',
            //   component: () => import('@/components/org/commonSelect'),
            //   isMust: true,
            //   isRequire: true,
            //   span: 8,
            // },
            {
              compKey: 'compKey5',
              labelName: '车系',
              codeField: 'carSeriesCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              isRequire: true,
              span: 8
              // validrule: {
              //   isformat: /^[A-Za-z0-9]{1,50}$/,
              //   resectMessage: {
              //     isformat: '请输入由数字和英文字母（不区分大小写）!'
              //   }
              // }
            }
          ],
      tableComponents2:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].formComponents
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].formComponents
          : [
            {
              compKey: 'compKey6',
              labelName: '用户ID',
              codeField: 'custNo',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              disabled: true,
              span: 8
            },
            {
              compKey: 'compKey7',
              labelName: '客户名称',
              codeField: 'custName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              isRequire: true,
              disabled: false,
              span: 8
            },
            {
              compKey: 'compKey18',
              labelName: '客户电话',
              codeField: 'phone',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              validrule: {
                isPhone: true,
                maxlength: 11
              },
              isMust: true,
              isRequire: true,
              disabled: false,
              span: 8
            },
            {
              compKey: 'compKey8',
              labelName: '性别',
              codeField: 'genderCode',
              customObj: {
                api: managements.queryXB,
                selectType: '1'
              },
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isRequire: true,
              isMust: true,
              disabled: false,
              span: 8
            },
            {
              compKey: 'compKey9',
              labelName: '省份',
              codeField: 'provinceId',
              textField: 'provinceName',
              oFields: 'provinceId,provinceName',
              component: () => import('@/components/org/commonSelect'),
              customObj: {
                api: managements.queryProvince,
                selectType: '1'
              },
              type: 'dropdownList',
              isRequire: true,
              disabled: false,
              isMust: true,
              isMul: false
            },
            {
              compKey: 'compKey10',
              labelName: '城市',
              codeField: 'cityId',
              textField: 'cityName',
              oFields: 'cityId,cityName',
              component: () => import('@/components/org/commonSelect'),
              customObj: {
                api: managements.queryCityList,
                listQuery: {
                  provinceId: ''
                },
                selectType: '1'
              },
              parentFileds: 'value:provinceId',
              type: 'dropdownList',
              disabled: false,
              isRequire: true,
              isMust: true
            },
            { // queryCityList
              compKey: 'compKey11',
              labelName: '区县',
              codeField: 'countyId',
              textField: 'countyName',
              oFields: 'countyId,countyName',
              component: () => import('@/components/org/commonSelect'),
              customObj: {
                api: managements.queryCountyList,
                listQuery: {
                  cityId: ''
                },
                selectType: '1'
              },
              parentFileds: 'value:cityId-cityId',
              type: 'dropdownList',
              isRequire: true,
              disabled: false,
              isMust: true,
              isMul: false
            },
            {
              compKey: 'compKey12',
              labelName: '详细地址',
              codeField: 'addrStreet',
              disabled: false,
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isRequire: true,
              isMust: true,
              span: 16
            },
            {
              compKey: 'compKey13',
              labelName: '出生日期',
              codeField: 'birthDate',
              component: () => import('@/components/org/datePicker/datePicker'),
              type: 'datePicker',
              isMust: true,
              isRequire: true,
              span: 8
            },
            {
              compKey: 'compKey14',
              labelName: '证件类型',
              codeField: 'credTypeCode',
              customObj: {
                api: managements.queryZJ,
                selectType: '1',
                defOptionFields: ['lookupValueCode', 'lookupValueName']
              },
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              disabled: false,
              isRequire: true,
              isMust: true,
              span: 8
            },
            {
              compKey: 'compKey15',
              labelName: '证件号码',
              codeField: 'credNo',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              disabled: false,
              isRequire: true,
              isMust: true,
              span: 8
            },
            {
              compKey: 'compKey16',
              labelName: '民族',
              codeField: 'nationCode',
              customObj: {
                api: managements.queryMZ,
                selectType: '1'
              },
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMust: true,
              disabled: false,
              isRequire: true,
              span: 8
            },
            {
              compKey: 'compKey17',
              labelName: '建档日期',
              codeField: 'createdDate',
              component: () => import('@/components/org/datePicker/datePicker'),
              type: 'datePicker',
              disabled: false,
              isRequire: true,
              isMust: true,
              span: 8
            }
          ],

      textMap: {
        edit: '他牌车详情',
        add: '客户车辆关系新增'
      },
      formField: {
        vin: '',
        carLicense: '',
        buyCaruseCode: '',
        buyCaruseText: '',
        carBrandCode: '',
        carBrandCn: '',
        carSeriesCode: '',

        custNo: '',
        phone: '',
        custName: '',
        genderCode: '',
        provinceId: '',
        provinceName: '',
        cityId: '',
        cityName: '',
        countyId: '',
        countyName: '',
        addrStreet: '',
        birthDate: '',
        credTypeCode: '',
        credNo: '',
        nationCode: '',
        createdDate: ''
      }
    }
  },
  watch: {
    curPopupsVisible: {
      handler(val) {
        if (val) {
          if (this.popupsState === 'edit') {
            this.tableComponents1.forEach((item) => {
              item.disabled = true
              item.isRequire = false
            })
            this.tableComponents2.forEach((item) => {
              item.disabled = true
              item.isRequire = false
            })
            this.tableButtons.splice(0, 1)
          } else {
          //  this.tableComponents1[3].codeField=''
          //  this.tableComponents1[3].textField=''
          }
        }
      },
      immediate: true
    }
  },
  created() {
    this.tableComponents = [...this.tableComponents1, ...this.tableComponents2]
  },
  methods: {
    closeEdit() {
      this.$emit('update:popupsVisible', false)
    },

    // 新增/编辑保存按钮
    save() {
      const that = this
      if (this.formField.vin.length > 17) {
        this.$message({
          message: 'VIN字符不可大于17',
          type: 'warn',
          duration: 2000
        })
        return
      }

      this.$utils.validataMoth(this, 'validpopup')
      if (this.valid) {
        if (that.popupsState === 'edit') {
          that.editSave()
        } else {
          that.addSave()
        }
      }
    },

    // 编辑保存
    editSave() {
      const that = this
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: managements.updateCustomerVehicle,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: that.formField

      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then(response => {
        if (response.result === '1') {
          this.$message({
            message: response.msg,
            type: 'success',
            duration: 1000
          })
          this.$emit('update:popupsVisible', false)
          this.$nextTick(() => {
            this.$parent.queryTable(1)
          })
        } else {
          this.$message({
            message: response.msg,
            type: 'warn',
            duration: 1000
          })
        }
      })
    },

    // 新增保存
    addSave() {
      const that = this
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: managements.insertCustomerVehicle,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: that.formField
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then(response => {
        if (response.result === '1') {
          this.$message({
            message: response.msg,
            type: 'success',
            duration: 1000
          })
          this.$emit('update:popupsVisible', false)
          this.$nextTick(() => {
            this.$parent.$refs.multipleTable.queryTable(1)
          })
        } else {
          this.$message({
            message: response.msg,
            type: 'warn',
            duration: 1000
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .filter-title{
    font-size: 18px;
    font-weight: bolder;
  }
  .filter-container.filter-title::before{
    border-left: 4px solid #0075FF;
    padding-right: 10px;
    height: 20px;
    left: 0px;
    top: 4px;
  }
</style>
